<template>
  <div class="layout">
    <div class="header">
      <div class="logo" @click="goIndex">EasyBlog</div>
      <el-menu
        :default-active="activeIndex"
        class="enu"
        mode="horizontal"
        router
        :ellipsis="false"
      >
        <el-menu-item
          v-for="item in menuList"
          :key="item.index"
          :index="item.path"
          @click="activeNavMenu(item.path)"
        >
          {{ item.title }}
        </el-menu-item>
      </el-menu>
    </div>
    <div class="body">
      <router-view></router-view>
    </div>
    <div class="footer">
      <div>
        ©2000-{{ new Date().getFullYear() }} {{ sysInfo.icpDomain }} rights
        reserved.{{ sysInfo.policeProvince }}ICP备{{ sysInfo.icpNo }}
      </div>
      <div>{{ sysInfo.policeProvince }}公网安备 {{ sysInfo.policeNo }}</div>
    </div>
  </div>
</template>

<script setup>
import { getCurrentInstance, reactive, ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import bus from './utils/EventBus'
const { proxy } = getCurrentInstance();
const router = useRouter();

onMounted(() => {
  getSysInfo();
  bus.on('toBlog',()=>{
    activeNavMenu('/blog')
  })
  bus.on('toCategory',()=>{
    activeNavMenu('/category')
  })
  bus.on('toUser',()=>{
    activeNavMenu('/users')
  })
  bus.on('toSpecial',()=>{
    activeNavMenu('/special')
  })
});

// 系统信息
const sysInfo = ref({});
const getSysInfo = async () => {
  let result = await proxy.Request({
    url: "/view/getSysInfo",
  });
  if (!result) {
    return;
  }
  sysInfo.value = result.data;
};

// 菜单列表
const menuList = reactive([
  {
    title: "博客",
    index: "1",
    path: "/blog",
  },
  {
    title: "分类专栏",
    index: "2",
    path: "/category",
  },
  {
    title: "专题",
    index: "3",
    path: "/special",
  },
  {
    title: "成员",
    index: "4",
    path: "/users",
  },
  {
    title: "留言板",
    index: "5",
    path: "/message",
  },
]);

// 激活菜单显示
let activeIndex = ref(
  window.sessionStorage.getItem("index")
    ? window.sessionStorage.getItem("index")
    : "/blog"
);
const activeNavMenu = (activedIndex) => {
  activeIndex.value = activedIndex;
  window.sessionStorage.setItem("index", activeIndex.value);
};

// 跳转主页
const goIndex = ()=>{
  router.push('../index')
  activeIndex.value='/index'
  window.sessionStorage.setItem("index", activeIndex.value);
}
</script>

<style lang="scss" scoped>
.layout {
  background-color: rgb(238, 235, 235);
  .header {
    padding-left: 20%;
    height: 80px;
    width: 80%;
    display: flex;
    box-shadow: 0 2px 6px 0 #ddd;
    background-color: #fff;

    .logo {
      padding: 0 20px;
      font-size: 30px;
      line-height: 80px;
      color: #79bbff;
      cursor: pointer;
    }
  }

  .body {    
    display: flex;
    justify-content: space-around;
    margin: 10px 20%;
    width: 60%;
    box-sizing: border-box;
    min-height: calc(100vh - 140px);
  }

  .footer {
    height: 40px;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
  }
}
</style>